package com.hyperf.core.util;

import com.github.dozermapper.core.DozerBeanMapperBuilder;
import com.github.dozermapper.core.Mapper;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author lizhaoyang
 */
public final class DozerMapperUtil {

    private static final Mapper mapper = DozerBeanMapperBuilder.buildDefault();

    public <T> T convert(Object source, Class<T> clazz) {
        return mapper.map(Optional.ofNullable(source), clazz);
    }

    public <T> List<T> convert(Collection<?> source, Class<T> clazz) {
        return Optional.ofNullable(source)
                .orElse(Collections.emptyList())
                .stream()
                .map(bean -> mapper.map(bean, clazz))
                .collect(Collectors.toList());
    }
}
